/**
 * Created by ljg on 2017/3/27.
 */

const searchInput=function () {
    angular.module('ui.help')
        .directive('uiUpload', function (eventHelper) {
            return {
                restrict: 'E',
                replace: true,
                transclude: true,
                controller:function ($scope,$attrs) {

                },
                scope:{
                    success:"&",
                    error:"&",
                    show: "&",
                    hide: "&"
                },
                template: function(el,attr){
                    const name=attr.name||'file';
                    return '<form method="post"><input class="btn btn-info" type="file" name="'+name+'"/></form>';
                },
                link : function(scope, element, attrs) {
                        $(element).find("input").bind('change',function () {
                            var showFunc = scope.show();
                            var hideFunc = scope.hide();
                            if(showFunc){
                                showFunc();
                            }
                            var formData = new FormData(element[0]);
                            $.ajax({
                                url: attrs.url ,
                                type: attrs.method||'POST',
                                data: formData,
                                async: false,
                                cache: false,
                                contentType: false,
                                processData: false,
                                success: function (returndata) {
                                    if(returndata.code=='0000'){
                                        const succFunc = scope.success();
                                        if(succFunc){
                                            succFunc(returndata);
                                        }else{
                                            toastr.success("上传成功");
                                        }
                                    }else{
                                        const errFunc=scope.error();
                                        if(errFunc){
                                            errFunc(returndata);
                                        }else{
                                            toastr.error("上传失败,"+returndata.msg);
                                        }

                                    }
                                    if (hideFunc){
                                        hideFunc();
                                    }
                                },
                                error: function (returndata) {
                                    toastr.error("上传失败");
                                    if (hideFunc){
                                        hideFunc();
                                    }
                                }
                            });
                        });
                },
            };
        });
}
export default searchInput;
